package com.bidevalution.business.function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.bidevalution.business.context.OpType;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
/**
 * 获取跟某个值的进行比较大小的所有数据
 * @author daizhifeng   
 * @date 2019年10月10日 下午9:40:34 
 * @version V2.0
 */
public class GetCompareOfArrayFunction extends AbstractFunction{
	private FunctionInitializer functionInitializer;
	public GetCompareOfArrayFunction(FunctionInitializer functionInitializer) {
		super();
		this.functionInitializer = functionInitializer;
	}
	/**
	 * 数组中在数据跟某个值进行比较，并返回比较之后的结果
	 * <p>Description: </p>
	 * @param env
	 * @param arg1 数组
	 * @param arg2 比较值
	 * @param arg3 比较符 lt 小于 gt 大于
	 * @return 返回例如:2,3,4
	 * @see com.googlecode.aviator.runtime.function.AbstractFunction#call(java.util.Map, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject)
	 */
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
		String paramValue = FunctionUtils.getStringValue(arg1, env);
		List<String> priceList = new LinkedList<String>(Arrays.asList(paramValue.split(",")));
		Double limitValue = Double.valueOf(arg2.getValue(env).toString());
		String compareSign = arg3.getValue(env).toString();
		List<String> tempList = new ArrayList<String>();
		System.out.println("tempListtempListtempLis************"+compareSign+limitValue);
		if(OpType.lt.name().equals(compareSign)) {
			for(String price:priceList) {
				if(!StringUtils.isEmpty(price)) {
					if(Double.valueOf(price).compareTo(limitValue)<0) {
						tempList.add(price);
					}
				}
				
			}
		}else if(OpType.gt.name().equals(compareSign)) {
			for(String price:priceList) {
				if(!StringUtils.isEmpty(price)) {
					if(Double.valueOf(price).compareTo(limitValue)>0) {
						tempList.add(price);
					}
				}
				
			}
		}
		System.out.println(tempList);
		
		return new AviatorString(StringUtils.collectionToDelimitedString(tempList, ","));
	}
	@Override
	public String getName() {
		return "getCompareOfArray";
	}

}
